<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/layui/css/public.css" media="all">
    <link rel="stylesheet" href="/layui/css/laydate.css" media="all">
    <link rel="stylesheet" href="/layui/css/layer.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

<%--        <fieldset class="table-search-fieldset">--%>
<%--            <legend>搜索信息</legend>--%>
<%--            <div style="margin: 10px 10px 10px 10px">--%>
<%--                <form class="layui-form layui-form-pane" action="">--%>
<%--                    <div class="layui-form-item">--%>
<%--                        <div class="layui-inline">--%>
<%--                            <label class="layui-form-label">用户姓名</label>--%>
<%--                            <div class="layui-input-inline">--%>
<%--                                <input type="text" name="username" autocomplete="off" class="layui-input">--%>
<%--                            </div>--%>
<%--                        </div>--%>
<%--                        <div class="layui-inline">--%>
<%--                            <label class="layui-form-label">用户性别</label>--%>
<%--                            <div class="layui-input-inline">--%>
<%--                                <input type="text" name="sex" autocomplete="off" class="layui-input">--%>
<%--                            </div>--%>
<%--                        </div>--%>
<%--                        <div class="layui-inline">--%>
<%--                            <label class="layui-form-label">用户身份证</label>--%>
<%--                            <div class="layui-input-inline">--%>
<%--                                <input type="text" name="card" autocomplete="off" class="layui-input">--%>
<%--                            </div>--%>
<%--                        </div>--%>
<%--                        <div class="layui-inline">--%>
<%--                            <label class="layui-form-label">用户电话</label>--%>
<%--                            <div class="layui-input-inline">--%>
<%--                                <input type="text" name="phone" autocomplete="off" class="layui-input">--%>
<%--                            </div>--%>
<%--                        </div>--%>
<%--                        <div class="layui-inline">--%>
<%--                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit--%>
<%--                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索--%>
<%--                            </button>--%>
<%--                        </div>--%>
<%--                    </div>--%>
<%--                </form>--%>
<%--            </div>--%>
<%--        </fieldset>--%>

    <div class="demoTable">
        搜索用户姓名：
        <div class="layui-inline">
            <input class="layui-input" name="onename" id="onename" autocomplete="off">
        </div>
        <button class="layui-btn" data-type="reload">搜索</button>
    </div>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<!--添加弹出层-->
<div id="Addforms" style="display: none;padding: 20px">
    <form id="Addform" method="post" class="layui-form layui-form-pane" lay-filter="adds">
        <div class="layui-form-item">
            <label class="layui-form-label">用户姓名</label>
            <div class="layui-input-block">
                <input type="text" name="username" class="layui-input" lay-verify="required" autocomplete="off"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户登录名</label>
            <div class="layui-input-block">
                <input type="text" name="loginname" class="layui-input" lay-verify="required" autocomplete="off"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户生日</label>
            <div class="layui-input-block">
                <input type="number" name="brithday" class="layui-input" lay-verify="required" autocomplete="off"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户密码</label>
            <div class="layui-input-block">
                <input type="text" name="password" class="layui-input" lay-verify="required" autocomplete="off"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户性别</label>
            <div class="layui-input-block">
                <select name="sex" lay-verify="required">
                    <option value="" disabled selected>==请选择==</option>
                    <option value="男">男</option>
                    <option value="女">女</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户爱好</label>
            <div class="layui-input-block">
                <input type="text" name="likes" class="layui-input" lay-verify="required" autocomplete="off"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户电话</label>
            <div class="layui-input-block">
                <input type="number" name="phone" class="layui-input" lay-verify="required" autocomplete="off"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户身份证号</label>
            <div class="layui-input-block">
                <input type="number" name="card" class="layui-input" lay-verify="required" autocomplete="off"/>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="addsubmit">提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>

<!--编辑弹出层-->
<div id="updateforms" style="display: none;padding: 20px">
    <form id="updatesform" method="post" class="layui-form layui-form-pane" lay-filter="updates">
        <input type="hidden" name="id"/>
        <div class="layui-form-item">
            <label class="layui-form-label">用户姓名</label>
            <div class="layui-input-block">
                <input type="text" name="username" class="layui-input" lay-verify="required" autocomplete="off"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户登录名</label>
            <div class="layui-input-block">
                <input type="text" name="loginname" class="layui-input" lay-verify="required" autocomplete="off"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户生日</label>
            <div class="layui-input-block">
                <input type="number" name="brithday" class="layui-input" lay-verify="required" autocomplete="off"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户密码</label>
            <div class="layui-input-block">
                <input type="text" name="password" class="layui-input" lay-verify="required" autocomplete="off"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户性别</label>
            <div class="layui-input-block">
                <input type="text" name="sex" class="layui-input" lay-verify="required" autocomplete="off"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户爱好</label>
            <div class="layui-input-block">
                <input type="text" name="likes" class="layui-input" lay-verify="required" autocomplete="off"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户电话</label>
            <div class="layui-input-block">
                <input type="text" name="phone" class="layui-input" lay-verify="required" autocomplete="off"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户身份证号</label>
            <div class="layui-input-block">
                <input type="text" name="card" class="layui-input" lay-verify="required" autocomplete="off"/>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="updatesubmit">提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script src="/layui/js/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        var tablein = table.render({
            elem: '#currentTableId',
            url: '/adminuserAll.do',
            toolbar: '#toolbarDemo',
            page:true,
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 80, title: 'ID', sort: true},
                {field: 'loginname', width: 80, title: '昵称'},
                {field: 'username', width: 80, title: '用户名'},
                {field: 'sex', width: 80, title: '性别', sort: true},
                {field: 'phone', width: 80, title: '电话'},
                {field: 'card', width: 150, title: '身份证号', sort: true},
                {field: 'brithday', width: 80, title: '生日'},
                {field: 'password', width: 80, title: '密码'},
                {field: 'likes', width: 80, title: '爱好'},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [5, 10, 15, 20, 25, 50],
            limit: 10,
            page: true,
            skin: 'line',
            id:'testReload'
        });

        // 监听搜索操作
        var $ = layui.$, active = {
            reload: function(){
                var noy = $('#onename').val();
                console.log(noy)
                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        loginname: noy
                    }
                },'data');
            }
        };

        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //添加first
        //监听表格头部工具栏事件
        table.on('toolbar(currentTableFilter)', function (obj) {
            console.log(obj)
            switch (obj.event) {
                case 'add':
                    openAddWindow();
                    break;
            }

        });

        //执行头部栏里的add选项
        var url;
        var mainIndex;

        function openAddWindow() {
            mainIndex = layer.open({
                type: 1,
                title: "添加用户",
                area: ['800px', '600px'],
                content: $("#Addforms"),
                success: function () {

                }
            });
        }

        //在弹窗中的操作
        form.on("submit(addsubmit)", function (data) {
            $.post(url = "/addadminuser.do", data.field, function (result) {
                if (result.success) {
                    layer.alert(result.message, {icon: 1});
                    layer.close(mainIndex);
                    tablein.reload();
                } else {
                    layer.alert(result.message, {icon: 2});
                }
            }, "json")

        });
        //添加end

        //更新first
        table.on('tool(currentTableFilter)', function (obj) {
            switch (obj.event) {
                case 'edit':
                    openUpdateWindow(obj.data);
                    break;
                case 'delete':
                    deletebyid(obj.data);
                    break;
            }


        });

        function openUpdateWindow(data) {
            mainIndex = layer.open({
                type: 1,
                title: "修改用户",
                area: ['800px', '600px'],
                content: $("#updateforms"),
                success: function () {
                    console.log("sdasjdhj");
                    form.val('updates', data);

                }

            });
        }

        form.on("submit(updatesubmit)", function (data) {
            $.post(url = "/updateadminuser.do", data.field, function (result) {
                if (result.success) {
                    layer.alert(result.message, {icon: 1});
                    layer.close(mainIndex);
                    tablein.reload();
                } else {
                    layer.alert(result.message, {icon: 2});
                }
            }, "json");

        });
        //更新end
        //删除first
        function deletebyid(data) {
            console.log(data)
            //提示用户是否删除
            layer.confirm("确定要删除吗[<font color='red'>" + data.loginname + "</font>]用户吗？", {
                icon: 3,
                title: "提示"
            }, function (index) {
                $.post("/deleteadminuser.do", {"id": data.id}, function (result) {
                    if (result.success) {
                        layer.alert(result.message, {icon: 1});
                        tablein.reload();
                    } else {
                        layer.alert(result.message, {icon: 2});
                    }
                }, "json")

                layer.close(index);
            });
        }

        //删除end
    });
</script>

</body>
</html>
